package cn.xinfei.xdecision.data.core.frame.executors.collect.handler;

import cn.xinfei.xdecision.data.core.frame.dispatch.ExecutorDispatch;
import cn.xinfei.xdecision.data.core.frame.executors.collect.VarHandler;
import cn.xinfei.xdecision.data.core.frame.executors.VarContext;
import cn.xinfei.xdecision.data.core.vo.VarCollectAwareVO;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;

@Service
@Slf4j
public class VarDispatchExecutorHandler implements VarHandler<VarCollectAwareVO> {


    @Autowired
    ExecutorDispatch executorDispatch;

    @Override
    public void doHandle(VarContext<VarCollectAwareVO> context) {
        VarCollectAwareVO varCollectAwareVO = context.get();
        Map<String, Object> executorResultMap = varCollectAwareVO.getExecutorResultMap();
        Map<String, Set<String>> derivedVarDependParamMap = varCollectAwareVO.getDerivedVarDependParamMap();
        executorDispatch.dispatch(
                varCollectAwareVO.getRequestId(),
                varCollectAwareVO.getVarCodes(),
                varCollectAwareVO.getInputPrarams(),
                varCollectAwareVO.getExecutorAndVarCodesMap(),
                derivedVarDependParamMap,
                executorResultMap,
                varCollectAwareVO.getCollectStageEnum()
        );
        log.info("requestId = {} executor dispatch done! collectStage = {} dispatchResult = {}",
                varCollectAwareVO.getRequestId(),
                varCollectAwareVO.getCollectStageEnum().getDesc(),
                JSONObject.toJSONString(executorResultMap));
        varCollectAwareVO.setExecutorResultMap(executorResultMap);
        context.setContext(varCollectAwareVO);
    }

}
